package com.yanqu.road.dao.impl.monopolymarket.fortress;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.fortress.MonopolyMarketFortressRankData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/10/20 21:59
 */
public class MonopolyMarketFortressRankDataDaoImpl extends TempDataObjectDao<MonopolyMarketFortressRankData> {

    public static String getMonopolyMarketFortressRankDataSql = "select * from `t_c_monopolymarket_fortress_rank` where `groupId`=? and `weekId`=?";
    public List<MonopolyMarketFortressRankData> getMonopolyMarketFortressRankData(long groupId, int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<MonopolyMarketFortressRankData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketFortressRankDataSql);
            int i = 1;
            call.setLong(i++, groupId);
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateMonopolyMarketFortressRankDataSql = "update `t_c_monopolymarket_fortress_rank` set totalValue=?,updateTime=?,rank=? where groupId=? and weekId=? and fortressId=? and unionId=?";
    @Override
    protected boolean updateData(MonopolyMarketFortressRankData monopolyMarketFortressRankData) {
        monopolyMarketFortressRankData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressRankData.getTotalValue()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRankData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRankData.getRank()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressRankData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRankData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRankData.getFortressId()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketFortressRankData.getUnionId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketFortressRankDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketFortressRankDataSql = "insert into `t_c_monopolymarket_fortress_rank`(groupId,weekId,fortressId,unionId,totalValue,updateTime,rank) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketFortressRankData monopolyMarketFortressRankData) {
        monopolyMarketFortressRankData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressRankData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRankData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRankData.getFortressId()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketFortressRankData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressRankData.getTotalValue()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRankData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRankData.getRank()));
        if (getDb().executeNoneQuery(insertMonopolyMarketFortressRankDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketFortressRankData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketFortressRankData monopolyMarketFortressRankData = new MonopolyMarketFortressRankData();
        monopolyMarketFortressRankData.setGroupId(rs.getLong("groupId"));
        monopolyMarketFortressRankData.setWeekId(rs.getInt("weekId"));
        monopolyMarketFortressRankData.setFortressId(rs.getInt("fortressId"));
        monopolyMarketFortressRankData.setUnionId(rs.getString("unionId"));
        monopolyMarketFortressRankData.setTotalValue(rs.getLong("totalValue"));
        monopolyMarketFortressRankData.setUpdateTime(rs.getInt("updateTime"));
        monopolyMarketFortressRankData.setRank(rs.getInt("rank"));

        return monopolyMarketFortressRankData;
    }
}
